package com.yunxi.poc.starbucks.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yunxi.poc.starbucks.common.BizConstant;
import com.yunxi.poc.starbucks.entity.SaleitemWithReportcategory;
import com.yunxi.poc.starbucks.entity.TStoreCapacity;
import com.yunxi.poc.starbucks.entity.Tb1Store;
import com.yunxi.poc.starbucks.entity.Tb1StoreOpen;
import com.yunxi.poc.starbucks.mapper.*;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Component
public class CacheUtil {
    // public static List<String> saleItemCodes = new ArrayList<>();
    public static final List<String> categories = new ArrayList<>();
    public static final List<String> storeIds = new ArrayList<>();
    public static final List<Tb1StoreOpen> storeOpens = new ArrayList<>();
    public static final List<Tb1Store> stores = new ArrayList<>();
    public static final Map<String, String> coordinateDistances = new Hashtable<>();
    public static Map<String, List<TStoreCapacity>> storesCapacity = new HashMap<>();
    @Resource
    private SaleitemWithReportcategoryMapper saleitemWithReportcategoryMapper;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private Tb1StoreOpenMapper storeOpenMapper;
    @Resource
    private Tb1StoreMapper storeMapper;
    @Resource
    private TStoreCapacityMapper storeCapacityMapper;
    @Resource
    private CoordinateDistanceMapper coordinateDistanceMapper;

    @PostConstruct
    public void init() {
        QueryWrapper<SaleitemWithReportcategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("report_category", BizConstant.REPORT_CATEGORY);

        // List<String> codes = this.saleitemWithReportcategoryMapper.saleItemCodes(queryWrapper);
        // saleItemCodes.addAll(codes);

        List<String> categoriesRes = this.saleitemWithReportcategoryMapper.categories(queryWrapper);
        categories.addAll(categoriesRes);
        categories.addAll(BizConstant.REPORT_CATEGORY);

        List<String> ids = orderMapper.ids(Wrappers.emptyWrapper());
        storeIds.addAll(ids);

        QueryWrapper<Tb1StoreOpen> openWrapper = new QueryWrapper<>();
        openWrapper.select("global_id", "day_of_week_mon1_sun7", "business_open_time", "business_close_time");
        storeOpens.addAll(storeOpenMapper.selectList(openWrapper));

        QueryWrapper<Tb1Store> storeWrapper = new QueryWrapper<>();
        storeWrapper.select("global_store_number", "latitude", "longitude", "store_name", "store_address");
        storeWrapper.eq("valid_flag", 1);
        stores.addAll(storeMapper.selectList(storeWrapper));

        QueryWrapper<TStoreCapacity> storeCpWrapper = new QueryWrapper<>();
        storeCpWrapper.select("store_id", "business_day", "max20min_sales", "start", "end");
        if (BizConstant.STORES_LIMIT) storeCpWrapper.in("store_id", BizConstant.TARGET_STORES);
        storesCapacity = storeCapacityMapper.selectList(storeCpWrapper)
                .parallelStream()
                .filter(e -> !BizConstant.HOLIDAYS.contains(e.getBusinessDay()))
                .collect(Collectors.groupingBy(TStoreCapacity::getStoreId));

        this.coordinateDistanceMapper.selectList(new QueryWrapper<>())
                .parallelStream()
                .forEach(e -> coordinateDistances.put(e.getCoordinate(), e.getDistance()));
    }
}
